﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using PSI.Models;
using PSI.Services;
using PSI.Filters;
using PSI.Helper;
using System.Reflection;

namespace PSI.Controllers {
    // 需要从PSIBaseController继承，因为部分Action需要不登陆就能调用
    public class UserController : PSIBaseController {
        private UserService UserService { get { return new UserService(); } }

        private PermissionService PermissionService { get { return new PermissionService(); } }

        public ActionResult Login(string returnURL) {
            if (PSI.Services.UserService.IsLogin) {
                return Redirect("/");
            }

            ViewBag.ReturnURL = returnURL;
            return View();
        }

        [HttpPost]
        public ActionResult LoginPOST(User user) {
            try {
                return Json(new UserService().Login(user));
            } catch (Exception ex) {
                DBHelper.Logger.Debug(ex.InnerException.Message);
                DBHelper.Logger.Debug(ex.StackTrace);

                return Json(new Result { Success = false, Messgae = "数据库服务没有启动" });
            }
        }

        [NeedLogin]
        public ActionResult Index() {
            if (!PermissionService.HasPermission(FIdConst.USER)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [NeedLogin]
        public ActionResult ChangeMyPassword() {
            var user = UserService.GetLoginUser();

            ViewBag.UserName = user != null ? user.Name : string.Empty;
            ViewBag.UserId = user != null ? user.Id : string.Empty;
            ViewBag.UserLoginName = user != null ? user.LoginName : string.Empty;


            return View();
        }

        [NeedLogin]
        [HttpPost]
        public ActionResult ChangeMyPasswordPOST(string id, string oldPassword, string newPassword) {
            var result = UserService.ChangeMyPassword(id, oldPassword, newPassword);

            return Json(result);
        }

        [NeedLogin]
        public ActionResult GetAllOrgs() {
            var result = new OrgService().BuildOrgTree();

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [NeedLogin]
        [HttpPost]
        public ActionResult GetUsers(string orgId) {
            var result = UserService.GetUsers(orgId);

            return Json(result);
        }

        [NeedLogin]
        [HttpPost]
        public ActionResult EditOrg(Org org) {
            try {
                var result = UserService.EditOrg(org);

                return Json(result);
            } catch (Exception ex) {
                DBHelper.Logger.Debug(ex.StackTrace);

                var result = new Result { Success = false, Messgae = ex.Message };
                return Json(result);
            }
        }

        [NeedLogin]
        [HttpPost]
        public ActionResult GetOrg(string id) {
            var result = UserService.GetOrg(id);
            return Json(result);
        }

        [NeedLogin]
        [HttpPost]
        public ActionResult DeleteOrg(string id) {
            var result = UserService.DeleteOrg(id);

            return Json(result);
        }

        [NeedLogin]
        [HttpPost]
        public ActionResult EditUser(User user) {
            var result = UserService.EditUser(user);

            return Json(result);
        }

        [NeedLogin]
        [HttpPost]
        public ActionResult DeleteUser(string id) {
            var result = UserService.DeleteUser(id);

            return Json(result);
        }

        [NeedLogin]
        [HttpPost]
        public ActionResult ChangePassword(string id, string password) {
            var result = UserService.ChangePassword(id, password);

            return Json(result);
        }
    }
}
